package com.koedia;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MyScanneurActivity extends Activity {
	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);


		try{
			// cas du 1er bouton => 1er voyage
			OnClickListener listenerFirstTravel = new OnClickListener()
			{
				public void onClick(View actuelView)
				{

					Intent intent = new Intent(MyScanneurActivity.this, DiplayTravel.class); 
					startActivity(intent);
				}
			};

			//On recupere le bouton souhaite et on lui affecte le Listener
			Button bouton1 = (Button) findViewById(R.id.firstTravel);
			bouton1.setOnClickListener(listenerFirstTravel);

		}catch ( ActivityNotFoundException e) {
			e.printStackTrace();
		}




		try{
			// cas du 2eme bouton => 2eme voyage
			OnClickListener listenerSecondTravel = new OnClickListener()
			{
				public void onClick(View actuelView)
				{

					Intent intent2 = new Intent(MyScanneurActivity.this, DisplayTravel2.class); 
					startActivity(intent2);
				}
			};

			Button button2 = (Button) findViewById(R.id.secondTravel);
			button2.setOnClickListener(listenerSecondTravel);

		}catch ( ActivityNotFoundException e) {
			e.printStackTrace();
		}



		try{
			// cas du 3eme bouton => 3eme voyage

			OnClickListener listenerThirdTravel = new OnClickListener()
			{
				public void onClick(View actuelView)
				{

					Intent intent3 = new Intent(MyScanneurActivity.this, DisplayTravel3.class); 
					startActivity(intent3);
				}
			};

			Button button3 = (Button) findViewById(R.id.thirdTravel);
			button3.setOnClickListener(listenerThirdTravel);

		}catch ( ActivityNotFoundException e) {
			e.printStackTrace();
		}



		try{


			// cas du bouton valider

			OnClickListener listenerBoutonValider = new OnClickListener()
			{
				public void onClick(View actuelView)
				{
					// on recupere le reference entre par l'utilisateur

					EditText editTextReference = (EditText) findViewById(R.id.saisi);
					String referenceVoyage = editTextReference.getText().toString();

					Intent intentValider = new Intent(MyScanneurActivity.this, DisplayInformation.class); 

					// on envoie le reference dans l'intent
					intentValider.putExtra("reference",referenceVoyage);
					startActivity(intentValider);
				}
			};

			Button buttonValider = (Button) findViewById(R.id.buttonValider);
			buttonValider.setOnClickListener(listenerBoutonValider);

		}catch ( ActivityNotFoundException e) {
			e.printStackTrace();
		}





		try {
			// cas du bouton scan
			OnClickListener listenerButtonScan = new OnClickListener()
			{
				public void onClick(View actuelView)
				{

					Intent intent4 = new Intent("com.google.zxing.client.android.SCAN");
					intent4.putExtra("SCAN_MODE", "QR_CODE_MODE");
					startActivityForResult(intent4, 0);// arguments (intent, requestCode)
	
				}
			};

			Button buttonScan = (Button) findViewById(R.id.buttonScan);
			buttonScan.setOnClickListener(listenerButtonScan);

		} catch (ActivityNotFoundException anfe) {
			Log.e("onCreate", "Scanner Not Found", anfe);
		}


	}// fin methode onCreate


	public void onActivityResult(int requestCode, int resultCode, Intent intent)
	{
		if (requestCode == 0) {

			TextView tvResult=(TextView)findViewById(R.id.tvResult);

			if (resultCode == RESULT_OK) {
				String contents = intent.getStringExtra("SCAN_RESULT");
				String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
				// Handle successful scan


				if (contents.startsWith("http://")){
					Uri uri = Uri.parse(contents); //contents returned by SCAN_RESULT
					Intent intentScan = new Intent(Intent.ACTION_VIEW, uri);  
					startActivity( intentScan );
					
				}
				else {
					tvResult.setText(intent.getStringExtra("SCAN_RESULT"));
					

				}

				//Toast toast = Toast.makeText(this, "Scan result :" + contents + " Format:" + format , Toast.LENGTH_LONG);
				//toast.setGravity(Gravity.TOP, 25, 400);
				//toast.show();
				
				
				
			} 
			else if (resultCode == RESULT_CANCELED) {
				// Handle cancel
				
				Toast toast = Toast.makeText(this, "Scan was Cancelled!", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 25, 400);
				toast.show();
				
			}
		}
	}



}
